<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 27, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "access.UserGroupList" );
RCoreImporter::cls( "base.SiteResourceList" );

/**
 * User group
 *
 */
class UserGroup {
	/**
	 * Group name
	 *
	 * @var string
	 */
	var $name;
	
	/**
	 * Group code
	 *
	 * @var string
	 */
	var $code;
	
	/**
	 * The child list of current Node
	 *
	 * @var UserGroupList
	 */
	var $_childrenGroups;
	
	/**
	 * List of SiteResource that this UserGroup can access directly
	 *
	 * @var SiteResourceList
	 */
	var $_accessedResources;
	
	/**
	 * List of SiteResource that user can't access to
	 *
	 * @var SiteResourceList
	 */
	var $_deniedResources;
	
	/**
	 * Constructor
	 *
	 * @param string $name
	 * @param string $code if code is null then the code will be equal to lowercase of $name
	 * @return UserGroup
	 */
	function UserGroup($name, $code = null) {
		$this->name = $name;
		if ($code === null) {
			$code = strtolower ( $name );
		}
		$this->code = $code;
		$this->_childrenGroups = new UserGroupList ( );
		$this->_accessedResources = new SiteResourceList ( );
		$this->_deniedResources = new SiteResourceList ( );
	}
	
	/**
	 * Get the children UserGroups of this UserGroup
	 *
	 * @return UserGroupList
	 */
	function &getChildrenGroups() {
		return $this->_childrenGroups;
	}
	
	/**
	 * Get all UserGroup that hold inside (included children groups)
	 *
	 * @return UserGroupList
	 */
	function getAllUserGroups() {
		return $this->_childrenGroups->getAllUserGroups ();
	}
	
	/**
	 * Add the SiteResource into this list
	 *
	 * @param SiteResource $resource
	 */
	function addAccessResource($resource) {
		$this->_accessedResources->addResource ( $resource );
	}
	
	/**
	 * Add denied resource into the dine list
	 *
	 * @param SiteResource $resource
	 */
	function addDineResource($resource) {
		$this->_deniedResources->addResource ( $resource );
	}
	
	/**
	 * Check if this UserGroup can access a resource or not
	 *
	 * @param SiteResource $resource
	 */
	function canAccess($resource) {
		if ($this->_deniedResources->checkInputCover ( $resource )) {
			return false;
		} else if ($this->_accessedResources->checkInputCover ( $resource )) {
			return true;
		}
		
		return $this->_childrenGroups->canAccess ( $resource );
	}
	
	/**
	 * Add a child group
	 *
	 * @param UserGroup $userGroup
	 */
	function addChildGroup($userGroup) {
		$this->_childrenGroups->addGroup ( $userGroup );
	}
}
?>